<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm" width="70%">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form v-if="!baTable.form.loading" ref="formRef" @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)" :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'" :rules="rules">
                    <FormItem :label="t('service.service.class_id')" type="remoteSelect"
                        v-model="baTable.form.items!.class_id" prop="class_id"
                        :input-attr="{ pk: 'ba_service_classification.class_id', field: 'class_name', 'remote-url': '/admin/service.Classification/index' }"
                        :placeholder="t('Please select field', { field: t('service.service.class_id') })" />
                    <FormItem :label="t('service.service.service_name')" type="string"
                        v-model="baTable.form.items!.service_name" prop="service_name"
                        :placeholder="t('Please input field', { field: t('service.service.service_name') })" />
                    <FormItem :label="t('service.service.service_title')" type="string"
                        v-model="baTable.form.items!.service_title" prop="service_title"
                        :placeholder="t('Please input field', { field: t('service.service.service_title') })" />
                    <FormItem :label="t('service.service.service_cover_img')" type="image"
                        v-model="baTable.form.items!.service_cover_img" prop="service_cover_img"
                        :placeholder="t('Please input field', { field: t('service.service.service_cover_img') })" />
                    <FormItem :label="t('service.service.service_carousel_imgs')" type="images"
                        v-model="baTable.form.items!.service_carousel_imgs" prop="service_carousel_imgs"
                        :placeholder="t('Please input field', { field: t('service.service.service_carousel_imgs') })" />
                    <FormItem :label="t('service.service.service_price')" type="number" prop="service_price"
                        :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_price"
                        :placeholder="t('Please input field', { field: t('service.service.service_price') })" />
                    <FormItem :label="t('service.service.sales_volume')" type="number" prop="sales_volume"
                        :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.sales_volume"
                        :placeholder="t('Please input field', { field: t('service.service.sales_volume') })" />


                    <FormItem :label="t('service.service.service_taboo_statement')" type="image"
                        v-model="baTable.form.items!.service_taboo_statement" prop="service_taboo_statement"
                        :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('service.service.service_taboo_statement') })" />
                    <FormItem :label="t('service.service.service_introduce')" type="image"
                        v-model="baTable.form.items!.service_introduce" prop="service_introduce"
                        :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('service.service.service_introduce') })" />

                    <FormItem :label="t('service.service.service_ordering_notice')" type="image"
                        v-model="baTable.form.items!.service_ordering_notice" prop="service_ordering_notice"
                        :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('service.service.service_ordering_notice') })" />
                    <FormItem :label="t('service.service.service_weight')" type="number" prop="service_weight"
                        :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.service_weight"
                        :placeholder="t('Please input field', { field: t('service.service.service_weight') })" />

                    <el-form-item :label="t('service.service.service_duration')" prop="service_duration">
                        <el-input type="number" v-model.number="baTable.form.items!.service_duration"
                            :placeholder="t('Please input field', { field: t('service.service.service_duration') })">
                            <template #append>分钟</template>
                        </el-input>
                    </el-form-item>

                    <el-form-item :label="t('service.service.distribution_rebate')" prop="distribution_rebate">
                        <el-input type="number" v-model.number="baTable.form.items!.distribution_rebate"
                            :placeholder="t('Please input field', { field: t('service.service.distribution_rebate') })">
                            <template #append>%</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item :label="t('加钟抽成比例')" prop="clock_distribution_rebate">
                        <el-input type="number" v-model.number="baTable.form.items!.clock_distribution_rebate"
                            :placeholder="t('Please input field', { field: t('加钟抽成比例') })">
                            <template #append>%</template>
                        </el-input>
                    </el-form-item>
                    <FormItem :label="t('service.service.service_status')" type="radio"
                        v-model="baTable.form.items!.service_status" prop="service_status" :data="{
                            content: {
                                0: '下架',
                                1: '上架'
                            }
                        }" :placeholder="t('Please select field', { field: t('service.service.service_status') })" />
                    <FormItem label="服务类型" v-if="adminInfo.id == 1" type="radio" v-model="baTable.form.items!.type"
                        prop="type" :data="{
                            content: {
                                1: '正常服务',
                                2: '加钟服务'
                            }
                        }" :placeholder="t('Please select field', { field: t('服务类型') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                            t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { useAdminInfo } from '/@/stores/adminInfo'
const adminInfo = useAdminInfo()

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

watch(() => baTable.form.operate, () => {
    if (baTable.form.operate === 'Add') {
        baTable.form.items!.service_status = 1
        baTable.form.items!.type = 1
    }
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    class_id: [buildValidatorData({ name: "required", title: t('service.service.class_id') })],
    service_name: [buildValidatorData({ name: "required", title: t('service.service.service_name') })],
    service_title: [buildValidatorData({ name: "required", title: t('service.service.service_title') })],
    service_cover_img: [buildValidatorData({ name: "required", title: t('service.service.service_cover_img') })],
    service_carousel_imgs: [buildValidatorData({ name: "required", title: t('service.service.service_carousel_imgs') })],
    service_price: [buildValidatorData({ name: "required", title: t('service.service.service_price') })],
    sales_volume: [buildValidatorData({ name: 'required', title: t('service.service.sales_volume') })],
    service_duration: [buildValidatorData({ name: 'required', title: t('service.service.service_duration') })],
    distribution_rebate: [buildValidatorData({ name: 'required', title: t('service.service.distribution_rebate') })],
    clock_distribution_rebate: [buildValidatorData({ name: 'required', title: t('加钟抽成') })],
    service_introduce: [buildValidatorData({ name: 'required', title: t('service.service.service_introduce') })],
    service_taboo_statement: [buildValidatorData({
        name: 'required',
        title: t('service.service.service_taboo_statement')
    })],
    service_ordering_notice: [buildValidatorData({
        name: 'required',
        title: t('service.service.service_ordering_notice')
    })],
    service_weight: [buildValidatorData({ name: 'number', title: t('service.service.service_weight') })],
})
</script>

<style scoped lang="scss"></style>
